final int NO_SCOPE = 1;
class Message 
{
  /**
  IMPLEMENT: convert String receiver to XBeeAddress64 receiver
  **/
  
  XBeeAddress64 receiver;
  int function;
  int scope;
  int[] arguments;
  
  Message() 
  {
    arguments  =  new int[0];
    scope      =  NO_SCOPE;  
    receiver   =  new XBeeAddress64( " 00 00 00 00 00 00 00 00 " );
    function   =  0;
  }
  
  /**
  Message( String rec, int func, int[] args )
  {
    
  }
  **/

  void clearScope( )
  {
    scope = NO_SCOPE;
  }  

  void setScope( int _scope )
  {
    scope = _scope;
  }
  
  void setScope( String _scope )
  {
    scope = hashGroup(_scope);
  }

  void setReceiver( int[] newReceiver )
  {
    receiver = new XBeeAddress64( newReceiver );
  }
  
  void setReceiver( int r )
  {
    receiver = nodes[r].addr64;
  }
  
  void setReceiver( String s ) {
    for (int i = 0; i < nodes.length; i++) {
      if (s.equals(nodes[i].name)) { receiver = nodes[i].addr64; }
    }
  }
  
  void setFunction(int f) 
  {
    function = f;
  }
  
  void addArgument(int a) 
  {
    arguments = (int[])expand(arguments, arguments.length + 2);
    arguments[arguments.length-2] = a >> 8 & 0xff;
    arguments[arguments.length-1] = a & 0xff;
  }
  
  void clearArguments() 
  {
    arguments = (int[]) expand(arguments, 0);
  }
  
  int getScope( )
  {
    return scope;
  }
  
  int[] getPayload() 
  {
    int payLength = 3 + arguments.length;
    int[] pl = new int[payLength];
    pl[0] = scope >> 8 & 0xff;
    pl[1] = scope & 0xff;
    pl[2] = function;
    for (int i = 0; i < arguments.length; i++) 
    {
      pl[i+3] = arguments[i];
    }
//    print("sending: ");
//    for (int i = 0; i < pl.length; i++) 
//    {
//      print(pl[i] + " ");
//    }
//    println();
    clearArguments(); // make sure we empty the current argus
    clearScope(); // and clear the scope
    return pl;    
  }
  
  int[] getAddress64()
  {  
    return receiver.getAddress();
  }
}

int hashGroup( String word )
{
  int wordValue  =  0;
  for( int i=0; i < word.length(); i++ )
  {
    wordValue  +=  byte( word.charAt(i) );
  }
  wordValue += word.length(); 
  wordValue += byte( word.charAt(0) );
  wordValue += byte( word.charAt( word.length() - 1 ) );
  //println("word value of "+word+" = "+wordValue);
  return wordValue;
}
